﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Validate
    {
        public int Add(KeRui.Model.Validate model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@UserName", SqlDbType.NVarChar, 50), new SqlParameter("@Type", SqlDbType.NVarChar, 50), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@RequestTime", SqlDbType.DateTime) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.UserID;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.Type;
            parameters[4].Value = model.State;
            parameters[5].Value = model.RequestTime;
            return DbHelperSQL.RunProcedure("Validate_ADD", parameters, out num);
        }

        public int Delete(int Id)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.Int, 4) };
            parameters[0].Value = Id;
            return DbHelperSQL.RunProcedure("Validate_Delete", parameters, out num);
        }

        public DataTable GetListByCondition(int userID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int) };
            parameters[0].Value = userID;
            DataSet set = DbHelperSQL.RunProcedure("[Validate_GetListBYCondition]", parameters, "ds");
            if (set.Tables.Count > 0)
            {
                return set.Tables[0];
            }
            return null;
        }

        public KeRui.Model.Validate GetModel(int Id)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.Int, 4) };
            parameters[0].Value = Id;
            KeRui.Model.Validate validate = new KeRui.Model.Validate();
            DataSet set = DbHelperSQL.RunProcedure("Validate_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    validate.Id = int.Parse(set.Tables[0].Rows[0]["Id"].ToString());
                }
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    validate.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                validate.UserName = set.Tables[0].Rows[0]["UserName"].ToString();
                validate.Type = set.Tables[0].Rows[0]["Type"].ToString();
                if (set.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    validate.State = int.Parse(set.Tables[0].Rows[0]["State"].ToString());
                }
                if (set.Tables[0].Rows[0]["RequestTime"].ToString() != "")
                {
                    validate.RequestTime = DateTime.Parse(set.Tables[0].Rows[0]["RequestTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["TransactTime"].ToString() != "")
                {
                    validate.TransactTime = DateTime.Parse(set.Tables[0].Rows[0]["TransactTime"].ToString());
                }
                validate.GUID = set.Tables[0].Rows[0]["GUID"].ToString();
                return validate;
            }
            return null;
        }

        public int Update(KeRui.Model.Validate model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.Int, 4), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@TransactTime", SqlDbType.DateTime) };
            parameters[0].Value = model.Id;
            parameters[1].Value = model.State;
            parameters[2].Value = model.TransactTime;
            return DbHelperSQL.RunProcedure("Validate_Update", parameters, out num);
        }

        public int UpdateState(KeRui.Model.Validate model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Type", SqlDbType.NVarChar, 100), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@RequestTime", SqlDbType.DateTime) };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.Type;
            parameters[2].Value = model.State;
            parameters[3].Value = model.RequestTime;
            return DbHelperSQL.RunProcedure("Validate_UpdateState", parameters, out num);
        }
    }
}

